//package com.yupi.springbootinit.controller;
//
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.qing.qingapiclientsdk.Client.QingApiClient;
//import com.qing.qingapicommon.model.entity.InterfaceInfo;
//import com.qing.qingapicommon.model.entity.UserInterfaceInfo;
//import com.qing.qingapicommon.model.vo.InterfaceInfoVO;
//import com.yupi.springbootinit.annotation.AuthCheck;
//import com.yupi.springbootinit.common.BaseResponse;
//import com.yupi.springbootinit.common.ErrorCode;
//import com.yupi.springbootinit.common.ResultUtils;
//import com.yupi.springbootinit.exception.BusinessException;
//import com.yupi.springbootinit.mapper.UserInterfaceInfoMapper;
//import com.yupi.springbootinit.service.InterfaceInfoService;
//import com.yupi.springbootinit.service.UserInterfaceInfoService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.util.CollectionUtils;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@RestController
//@RequestMapping("/analysis")
//@Slf4j
//public class AnalysisController {
//
//    @Resource
//    private UserInterfaceInfoMapper userInterfaceInfoMapper;
//
//    @Resource
//    private InterfaceInfoService interfaceInfoService;
//
//    @GetMapping("/top/interface/invoke")
//    @AuthCheck(mustRole = "admin")
//    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo() {
//        // 这里就继承InterfaceInfo，再补充一个调用次数的字段}
////        查询调用次数最多的接口信息列表
//        List<UserInterfaceInfo> interfaceInfoList = userInterfaceInfoMapper.listTopInvokeInterfaceInfo(3);
//        // 将接口信息按照接口ID分组，便于关联查询
//        Map<Long, List<UserInterfaceInfo>> interfaceInfoIdObjMap = interfaceInfoList.stream()
//                .collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));
////        Map<Long, List<InterfaceInfo>> interfaceInfoIdObjMap1 = interfaceInfoList.stream()
////                .collect(Collectors.groupingBy(InterfaceInfo::getId));
//        // 创建查询接口信息的条件包装器
//        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
//        // 设置查询条件，使用接口信息ID在接口信息映射中的键集合进行条件匹配
//        queryWrapper.in("id", interfaceInfoIdObjMap.keySet());
//
//        // 调用接口信息服务的list方法，传入条件包装器，获取符合条件的接口信息列表
//        List<InterfaceInfo> list = interfaceInfoService.list(queryWrapper);
//        // 判断查询结果是否为空
//        if (CollectionUtils.isEmpty(list)) {
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
//        }
//        // 构建接口信息VO列表，使用流式处理将接口信息映射为接口信息VO对象，并加入列表中
//        List<InterfaceInfoVO> interfaceInfoVOList = list.stream().map(interfaceInfo -> {
//            // 创建一个新的接口信息VO对象
//            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
//            // 将接口信息复制到接口信息VO对象中
//            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
//            // 从接口信息ID对应的映射中获取调用次数
//            int totalNum = interfaceInfoIdObjMap.get(interfaceInfo.getId()).get(0).getTotalNum();
////            String name = interfaceInfoIdObjMap.get(interfaceInfo.getId()).get(1).g;
//            // 将调用次数设置到接口信息VO对象中
//            interfaceInfoVO.setTotalNum(totalNum);
//            interfaceInfoVO.setName(interfaceInfo.getName());
//            // 返回构建好的接口信息VO对象
//            return interfaceInfoVO;
//
//        }).collect(Collectors.toList());
//
//        // 返回处理结果
//        return ResultUtils.success(interfaceInfoVOList);
//    }
//}
package com.qing.springbootinit.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.qingapicommon.model.entity.InterfaceInfo;
import com.qing.qingapicommon.model.entity.UserInterfaceInfo;
import com.qing.qingapicommon.model.vo.InterfaceInfoVO;
import com.qing.springbootinit.annotation.AuthCheck;
import com.qing.springbootinit.common.BaseResponse;
import com.qing.springbootinit.common.ErrorCode;
import com.qing.springbootinit.common.ResultUtils;
import com.qing.springbootinit.exception.BusinessException;
import com.qing.springbootinit.mapper.UserInterfaceInfoMapper;
import com.qing.springbootinit.service.InterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分析控制器
 * @author qingfengzy
 */
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo() {
        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoMapper.listTopInvokeInterfaceInfo(3);
        Map<Long, List<UserInterfaceInfo>> interfaceInfoIdObjMap = userInterfaceInfoList.stream()
                .collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", interfaceInfoIdObjMap.keySet());
        List<InterfaceInfo> list = interfaceInfoService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        List<InterfaceInfoVO> interfaceInfoVOList = list.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            int totalNum = interfaceInfoIdObjMap.get(interfaceInfo.getId()).get(0).getTotalNum();
            interfaceInfoVO.setTotalNum(totalNum);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(interfaceInfoVOList);
    }
}